#!/usr/bin/env bash

set -e

export CHROMA_PORT=8000

function cleanup {
  docker compose -f docker-compose.test.yml down --rmi local --volumes
  rm server.htpasswd .chroma_env
}

function setup_auth {
  local auth_type="$1"
  case "$auth_type" in
    basic)
      docker run --rm --entrypoint htpasswd httpd:2 -Bbn admin admin > server.htpasswd
      cat <<EOF > .chroma_env
CHROMA_SERVER_AUTH_CREDENTIALS_FILE="/chroma/server.htpasswd"
CHROMA_SERVER_AUTH_CREDENTIALS_PROVIDER="chromadb.auth.providers.HtpasswdFileServerAuthCredentialsProvider"
CHROMA_SERVER_AUTH_PROVIDER="chromadb.auth.basic.BasicAuthServerProvider"
EOF
      ;;
    token)
      cat <<EOF > .chroma_env
CHROMA_SERVER_AUTH_CREDENTIALS="test-token"
CHROMA_SERVER_AUTH_TOKEN_TRANSPORT_HEADER="AUTHORIZATION"
CHROMA_SERVER_AUTH_CREDENTIALS_PROVIDER="chromadb.auth.token.TokenConfigServerAuthCredentialsProvider"
CHROMA_SERVER_AUTH_PROVIDER="chromadb.auth.token.TokenAuthServerProvider"
EOF
      ;;
    xtoken)
          cat <<EOF > .chroma_env
CHROMA_SERVER_AUTH_CREDENTIALS="test-token"
CHROMA_SERVER_AUTH_TOKEN_TRANSPORT_HEADER="X_CHROMA_TOKEN"
CHROMA_SERVER_AUTH_CREDENTIALS_PROVIDER="chromadb.auth.token.TokenConfigServerAuthCredentialsProvider"
CHROMA_SERVER_AUTH_PROVIDER="chromadb.auth.token.TokenAuthServerProvider"
EOF
    ;;
    *)
      echo "Unknown auth type: $auth_type"
      exit 1
      ;;
  esac
}

trap cleanup EXIT

docker compose -f docker-compose.test.yml up --build -d

export CHROMA_INTEGRATION_TEST_ONLY=1
export CHROMA_API_IMPL=chromadb.api.fastapi.FastAPI
export CHROMA_SERVER_HOST=localhost
export CHROMA_SERVER_HTTP_PORT=8000
export CHROMA_SERVER_NOFILE=65535

echo testing: python -m pytest "$@"
python -m pytest "$@"

cd clients/js

# moved off of yarn to npm to fix issues with jackspeak/cliui/string-width versions #1314
npm install
npm run test:run

docker compose down
cd ../..
for auth_type in basic token xtoken; do
  echo "Testing $auth_type auth"
  setup_auth "$auth_type"
  cd clients/js
  docker compose --env-file ../../.chroma_env -f ../../docker-compose.test-auth.yml up --build -d
  yarn test:run-auth-"$auth_type"
  cd ../..
  docker compose down
done
